package com.liming.web.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.liming.domain.Book;
import com.liming.domain.BuyCarItem;
import com.liming.domain.BuyListBean;
import com.liming.domain.BuyListItem;
import com.liming.utils.DBUtils;

public class BuyList extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id=request.getParameter("id");
		String sql="select B_name,B_src,order_number,order_price,buy_num,buy_time from Buylist,Books where Buylist.B_id=Books.B_id and U_username=\'"+id+"\'";
		System.out.println(sql);
		Connection conn=DBUtils.open();
		BuyListBean buylist=new BuyListBean();
		try {
			Statement s=conn.createStatement();
			ResultSet re= s.executeQuery(sql);
			while(re.next()){
			BuyListItem item=new BuyListItem();
			item.setBname(re.getString("B_name"));
			item.setBsrc(re.getString("B_src"));
			item.setOrderNum(re.getString("order_number"));
			item.setOrderPrice(re.getString("order_price"));
			item.setBuyNum(re.getString("buy_num"));
			Timestamp t=re.getTimestamp("buy_time");
			Date date=new Date(t.getTime());
			String buyTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").format(date);
			item.setBuyTime(buyTime);
			buylist.getItemmap().put(item.getOrderNum(), item);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.getSession().setAttribute("buylist", buylist);
		request.getRequestDispatcher("/WEB-INF/jsp/hisbuylist.jsp").forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);

	}

}
